﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class CButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    [HideInInspector]
    public Button button = null;
    [HideInInspector]
    public Image image = null;
    private Vector3 originScale = Vector3.one;
    private Tweener tw = null;
    public bool isTween = true;

    private void Awake()
    {
        button = transform.GetComponent<Button>();
        image = transform.GetComponent<Image>();
        originScale = transform.localScale;
    }

    private void OnEnable()
    {
        if (!isTween) return;
        if (tw != null) tw.Kill();
        transform.localScale = originScale;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (!isTween) return;
        if (!button.interactable) return;
        if (tw != null) tw.Kill();
        tw = transform.DOScale(originScale * 0.9f, 0.2f);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (!isTween) return;
        if (!button.interactable) return;
        if (tw != null) tw.Kill();
        tw = transform.DOScale(originScale, 0.2f);
    }

    public void SetGray(bool b)
    {
        Util.SetUIGray(image,b);
    }
}
